// JavaScript Document

function LodePage(){
    $(".functionBox-boll").attr("onMouseMove","this.className='functionBox-boll active'");
    $(".functionBox-boll").attr("onMouseOut","this.className='functionBox-boll'");
    $("#matchBox>img").attr("onMouseMove","this.style.opacity=1");
    $("#matchBox>img").attr("onMouseOut","this.style.opacity=0.7");
    $("#matchBox>img").click(function () {
        $("#matchBox").parent().hide();
    });
    $("#createMatch>img").attr("onMouseMove","this.style.opacity=1");
    $("#createMatch>img").attr("onMouseOut","this.style.opacity=0.7");
    $("#createMatch>img").click(function () {
        $("#matchBox").parent().hide();
        $("#createMatch").hide();
        $("#matchBox").show();
    });

    $(".functionBox-boll:last").attr("onMouseMove","");
    $(".functionBox-boll:last").css("cursor","auto");

    $(".functionBox-boll:first").click(function(){
        $("#matchBox").parent().fadeIn(200);
    });

    $("#matchBox>ul>li").click(function () {
        if(this.innerText === "创建比赛"){
            $("#matchBox>table").hide();
            $("#createMatch").show();
            $("#matchBox>ul>.active").attr("class","");
            this.className = "active";
        }else if(this.innerText === "所有比赛"){
            $("#matchBox>ul>.active").attr("class","");
            this.className = "active";
            $("#matchBox>table").hide();
            $("#matchBox>table:first").show();
            $("#createMatch").hide();
        }else if(this.innerText === "我的比赛"){
            $("#matchBox>ul>.active").attr("class","");
            $("#matchBox>table").hide();
            $("#createMatch").hide();
            $("#matchBox>table:last").show();
            this.className = "active";
        }
    });

    var $myTable = $("#matchBox>table:last tr");
    for(let i = 1; i < $myTable.length; i++){
        let endTime = new Date($($myTable[i]).children("td:eq(3)").text().replace(/-/g, "/"));
        var nowData = new Date();
        if (endTime.getTime() + 86400000 < nowData.getTime() && $($myTable[i]).children("td:eq(5)").text() === "正在报名") {
            let $thisDiv = $($myTable[i]).children("td:eq(5)");
            $thisDiv.text("");
            $thisDiv.css("textAlign","left");
            $($myTable[i]).children("td").css("paddingTop","15px");
            $thisDiv.append("<button class='btn btn-danger'>报名过期</button>");
            $thisDiv.css({"paddingBottom":"0px","paddingTop":"8px"});
            var postData = $($myTable[i]).children("td:last").text();
            $.ajax({
                async: false,
                url: serverIpAddress+"Match/matchDataPass",
                data: postData,
                type: "post",
                contentType: "application/text;charset=utf-8"
            });
            $thisDiv.text("");
            $thisDiv.css("textAlign","left");
            $($myTable[i]).children("td").css("paddingTop","15px");
            $thisDiv.append("<button class='btn btn-danger'>报名截止</button>");
            $thisDiv.css({"paddingBottom":"0px","paddingTop":"8px","padding-left":"0px"});
        }else if($($myTable[i]).children("td:eq(5)").text() === "报名结束"){
            let $thisDiv = $($myTable[i]).children("td:eq(5)");
            $thisDiv.text("");
            $thisDiv.css("textAlign","left");
            $($myTable[i]).children("td").css("paddingTop","15px");
            $thisDiv.append("<button class='btn btn-warning'>开始比赛</button>");
            $thisDiv.css({"paddingBottom":"0px","paddingTop":"8px","padding-left":"0px"});
        }else if($($myTable[i]).children("td:eq(5)").text() === "已完成"){
            let $thisDiv = $($myTable[i]).children("td:eq(5)");
            $thisDiv.text("");
            $thisDiv.css("textAlign","left");
            $($myTable[i]).children("td").css("paddingTop","15px");
            $thisDiv.append("<button class='btn btn-success'>查看结果</button>");
            $thisDiv.css({"paddingBottom":"0px","paddingTop":"8px","padding-left":"0px"});
        }else if($($myTable[i]).children("td:eq(5)").text() === "报名截止"){
            let $thisDiv = $($myTable[i]).children("td:eq(5)");
            $thisDiv.text("");
            $thisDiv.css("textAlign","left");
            $($myTable[i]).children("td").css("paddingTop","15px");
            $thisDiv.append("<button class='btn btn-danger'>报名过期</button>");
            $thisDiv.css({"paddingBottom":"0px","paddingTop":"8px","padding-left":"0px"});
        }
    }

    var nowOptionMatchId = "";
    var isMatchBegin = true;
    $("#matchBox>table:last button").click(function () {
       if(this.innerText === "开始比赛"){
           if(isMatchBegin) {
               alert("比赛开始，请耐心等待结果！");
               $.ajax({
                   async: true,
                   data: $(this).parent().parent().children("td:last").text(),
                   type: "post",
                   contentType: "application/text;charset=utf-8",
                   url: serverIpAddress+"Match/startMatch"
               });
               isMatchBegin = false;
           }else{
               alert("已经开始比赛，请手动刷新页面");
           }

       }else if(this.innerText === "报名过期"){
           $("#changeDate").parent().show();
           nowOptionMatchId = $(this).parent().parent().children("td:last").text();
       }else if(this.innerText === "查看结果"){
           let matchId = $(this).parent().parent().children("td:last").text();
           window.location = "MatchResult.html?id=" + matchId;
       }
    });

    $("#changeDate>button").click(function () {
        var nowData = new Date();
        var endTime = new Date($(this).parent().children("input").val().replace(/-/g,"/"));
        if($(this).parent().children("input").val() === "" || endTime.getTime() - nowData.getTime() < 259200000)
        {
            alert("报名持续时间最少为三天！");
        }else {
            let postValue = {};
            postValue["id"] = nowOptionMatchId;
            postValue["endTime"] = $(this).parent().children("input").val();
            $.ajax({
                async: true,
                type: "post",
                url: serverIpAddress+"Match/changeMatchEndTime",
                data: JSON.stringify(postValue),
                contentType: "application/json;charset=utf-8"
            });
            alert("信息已提交！");
            window.location = "SuperUser.html";
        }
    });

    $("#createMatch button").click(function(){
        var createMatch = {};
        var $inputList = $("#createMatch input");
        createMatch["title"] = $($inputList[0]).val();
        createMatch["userNumber"] = Number($($inputList[1]).val());
        createMatch["type"] = $("#createMatch select").val();
        createMatch["endTime"] = $($inputList[2]).val();
        createMatch["describe"] = $("#createMatch textarea").val();

        var nowData = new Date();
        var endTime = new Date(createMatch["endTime"].replace(/-/g,"/"));

        if(!createMatch["userNumber"] || createMatch["userNumber"] > 20 || createMatch["userNumber"] < 1)
        {
            alert("参赛人数输入格式错误！");
        }else if(endTime.getTime() - nowData.getTime() < 259200000)
        {
            alert("报名持续时间最少为三天！");
        }else
        {
            createMatch["userToken"] = localStorage.getItem("superUserToken");
            $.ajax({
                async: false,
                type: "post",
                url: serverIpAddress+"Match/saveMatchMessage",
                data: JSON.stringify(createMatch),
                contentType: "application/json;charset=utf-8"
            });
            alert("信息已提交！");
            window.location = "SuperUser.html";
        }
    });
}



